﻿
(function () {
    appModule.controller('tenant.views.announcement.createOrEditAnnouncement', [
        '$scope', '$uibModalInstance', 'abp.services.app.announcement', 'announcementId', 'appSession','abp.services.app.user',
        function ($scope, $uibModalInstance, announcementService, announcementId, appSession, userService) {
            var vm = this;
            
            vm.saving = false;
            vm.announcement = null;

            //编辑框输出转码
            vm.htmlDecodeByRegExp = function (str) {
                var s = "";
                if (!str) return "";
                if (str.length == 0) return "";
                s = str.replace(/&amp;/g, "&");
                s = s.replace(/&lt;/g, "<");
                s = s.replace(/&gt;/g, ">");
                s = s.replace(/&nbsp;/g, " ");
                s = s.replace(/&#39;/g, "\'");
                s = s.replace(/&quot;/g, "\"");
                return s;
            }
            //编辑器工具
            vm.editor = {
                tools: [
                    "bold", "italic", "underline", "strikethrough", "justifyLeft",
                    "justifyCenter", "justifyRight", "justifyFull", "insertUnorderedList",
                    "insertOrderedList", "indent", "outdent", "createLink", "unlink",
                    "insertImage", "insertFile", "subscript", "superscript", "createTable",
                    "addRowAbove", "addRowBelow", "addColumnLeft", "addColumnRight", "deleteRow",
                    "deleteColumn", "viewHtml", "formatting",
                    "cleanFormatting", "fontName", "fontSize", "foreColor", "backColor", "print"
                ]
            }

            vm.save = function (nub) {
                vm.saving = true;
                vm.announcement.status = nub;
                vm.announcement.userid = appSession.user.id;
                announcementService.createOrUpdateAnnouncement({ announcement: vm.announcement }).success(function () {
                    if (nub == 1) {
                        //获取所有用户
                        userService.getUsers({}).success(function (result) {
                            var ids = [];
                            for (var i = 0; i < result.items.length; i++){
                                ids.push(result.items[i].id);
                            }
                            var misPostDatas = {};
                            //{ title: vm.title, content: vm.content, userid: appSession.user.id, ids: vm.ids }ids: ["2", "3", "4", "5", "6", "7", "8", "9"]
                            misPostDatas.title = '系统公告信息';
                            misPostDatas.content = '有新的公告发布，请注意查看！';
                            misPostDatas.userid = appSession.user.id;
                            misPostDatas.ids = ids;
                            announcementService.sendAnnouncement(misPostDatas).success(function () {
                                abp.notify.info("已成功保存并发布！");
                                $uibModalInstance.close();
                            }).finally(function () {
                                vm.saving = false;
                            });
                        }).finally(function () {
                            vm.saving = false;
                        });
                    } else {
                        abp.notify.info(app.localize('SavedSuccessfully'));
                        $uibModalInstance.close();
                    }

                }).finally(function () {
                    vm.saving = false;
                });
            };

            vm.cancel = function () {
                $uibModalInstance.dismiss();
            };
            function init() {
                announcementService.getAnnouncementForEdit({
                    id: announcementId
                }).success(function (result) {
                    vm.announcement = result.announcement;
                    if (vm.announcement) {
                        vm.announcement.content = vm.htmlDecodeByRegExp(vm.announcement.content);
                    } else {
                        vm.announcement = {};
                        vm.announcement.content = "在此填入公告内容···";
                    };
                });
            }

            init();
        }
    ]);
})();
